import 'dart:ui';

import 'package:animation_effect_demo/src/effects/text_operation.dart';
import 'package:flutter/material.dart';

import 'base/base_effect.dart';
import 'image_effect/image_operation.dart';

class EffectTemplate {
  String uuid = '';

  ///模板名称
  String name = '';

  late bool isFavorite;

  late bool isMyTemplate;

  ///模板效果类型
  late EffectType effectType;

  ///模板设置参数
  EffectOperation? operation;

  EffectTemplate(this.uuid, this.name,
      {required this.effectType,
      this.isFavorite = false,
      this.isMyTemplate = false,
      this.operation});

  EffectTemplate copyWith(
      {String? uuid,
      String? name,
      EffectType? effectType,
      bool? isFavorite,
      bool? isMyTemplate,
      EffectOperation? operation}) {
    return EffectTemplate(uuid ?? this.uuid, name ?? this.name,
        effectType: effectType ?? this.effectType,
        isFavorite: isFavorite ?? this.isFavorite,
        isMyTemplate: isMyTemplate ?? this.isMyTemplate,
        operation: operation ?? this.operation);
  }

  EffectTemplate.fromJson(Map<String, dynamic> json) {
    uuid = json['uuid'];
    name = json['name'];
    isFavorite = json['isFavorite'];
    isMyTemplate = json['isMyTemplate'];
    effectType = EffectType.values[json['effectType']];
    operation = json['operation'] != null
        ? EffectOperation.fromJson(json['operation'])
        : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['uuid'] = uuid;
    data['name'] = name;
    data['isFavorite'] = isFavorite;
    data['isMyTemplate'] = isMyTemplate;
    data['effectType'] = effectType.index;
    if (operation != null) {
      data['operation'] = operation?.toJson();
    }
    return data;
  }
}

enum SmearType { static, dynamic }

///静态模板动画类型
enum DynamicType {
  fixed,
  flicker,
  shiftLeft,
  shiftRight,
  shiftTop,
  shiftBottom,
}

enum SpeedOrientation { vertical, horizontal }

class SmearPoint {
  int x = 0;
  int y = 0;
  SmearType type = SmearType.static;
  Color color = Colors.transparent;

  SmearPoint({required this.type, required this.x, required this.y,this.color = Colors.transparent});

  SmearPoint.fromJson(Map<String, dynamic> json) {
    x = json['x'];
    y = json['y'];
    type = SmearType.values[json['type']];
    color = Color(json['color']);
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['x'] = x;
    data['y'] = y;
    data['type'] = type.index;
    data['color'] = color.value;
    return data;
  }
}

class SmearOperation {
  int vertical = 0;
  int horizontal = 0;
  int rotation = 0;
  List<SmearPoint> dynamicPoints = [];

  SmearOperation({
    this.vertical = 0,
    this.horizontal = 0,
    this.rotation = 0,
  });

  SmearOperation.fromJson(Map<String, dynamic> json) {
    vertical = json['vertical'];
    horizontal = json['horizontal'];
    rotation = json['rotation'];
    if (json['dynamicPoints'] != null) {
      dynamicPoints = <SmearPoint>[];
      json['dynamicPoints'].forEach((v) {
        dynamicPoints.add(SmearPoint.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['vertical'] = vertical;
    data['horizontal'] = horizontal;
    data['rotation'] = rotation;
    data['dynamicPoints'] = dynamicPoints.map((v) => v.toJson()).toList();
    return data;
  }
}


class EffectOperation {
  ///模板颜色列表
  // List<List<Color>>? colorTemplateList;

  ///颜色列表index,根据index从colorTemplateList获取颜色列表
  int? colorIndex;

  ///颜色列表
  List<Color>? colorList;

  ///速度
  int? speed;

  ///颜色宽度
  int? colorWidth;

  ///角度
  int? angle;

  ///密度
  int? density;

  ///涂抹设置
  SmearOperation? smearOperation;

  ///静态模板动画类型
  // DynamicType? dynamicType;

  ///速度方向（垂直或水平）
  SpeedOrientation? speedOrientation;

  ///文本DIY内容
  TextOperation? textOperation;

  ///图片动效
  ImageOperation? imageOperation;

  EffectOperation(
      {
      // this.colorTemplateList,
      this.colorList,
      this.colorIndex,
      this.speed,
      this.colorWidth,
      this.angle,
      this.density,
      // this.dynamicType,
      this.speedOrientation,
      this.textOperation,
      this.imageOperation,
      this.smearOperation});

  EffectOperation copyWith({
    // List<List<Color>>? colorTemplateList,
    List<Color>? colorList,
    int? colorIndex,
    int? speed,
    int? colorWidth,
    int? angle,
    int? density,
    // DynamicType? dynamicType,
    SpeedOrientation? speedOrientation,
    String? textContent,
    SmearOperation? smearOperation,
    TextOperation? textOperation,
    ImageOperation? imageOperation,
  }) {
    return EffectOperation(
        // colorTemplateList: colorTemplateList ?? this.colorTemplateList,
        colorList: colorList ?? this.colorList,
        colorIndex: colorIndex ?? this.colorIndex,
        speed: speed ?? this.speed,
        colorWidth: colorWidth ?? this.colorWidth,
        angle: angle ?? this.angle,
        density: density ?? this.density,
        // dynamicType: dynamicType ?? this.dynamicType,
        speedOrientation: speedOrientation ?? this.speedOrientation,
        textOperation: textOperation ?? this.textOperation,
        imageOperation: imageOperation ?? this.imageOperation,
        smearOperation: smearOperation ?? this.smearOperation);
  }

  EffectOperation.fromJson(Map<String, dynamic> json) {
    speed = json['speed'];
    colorWidth = json['colorWidth'];
    angle = json['angle'];
    density = json['density'];

    // if (json['dynamicType'] != null) {
    //   dynamicType = DynamicType.values[json['dynamicType']];
    // }

    if (json['speedOrientation'] != null) {
      speedOrientation = SpeedOrientation.values[json['speedOrientation']];
    }

    colorIndex = json['colorIndex'];

    // if (colorTemplateList != null) {
    //   colorTemplateList = (json['colorTemplateList'] as List).map((e) {
    //     return (e as List).map((e) => Color(e)).toList();
    //   }).toList();
    // }

    if (json['colorList'] != null) {
      colorList = (json['colorList'] as List).map((e) {
        return Color(e);
      }).toList();
    }

    textOperation = json['textOperation'] != null
        ? TextOperation.fromJson(json['textOperation'])
        : null;

    imageOperation = json['imageOperation'] != null
        ? ImageOperation.fromJson(json['imageOperation'])
        : null;

    smearOperation = json['smearOperation'] != null
        ? SmearOperation.fromJson(json['smearOperation'])
        : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['speed'] = speed;
    data['colorWidth'] = colorWidth;
    data['angle'] = angle;
    data['density'] = density;
    // data['dynamicType'] = dynamicType?.index;
    data['speedOrientation'] = speedOrientation?.index;
    data['colorIndex'] = colorIndex;
    data['textOperation'] = textOperation?.toJson();
    data['imageOperation'] = imageOperation?.toJson();
    // data['colorTemplateList'] = colorTemplateList?.map((e) => e.map((e) => e.value).toList()).toList();
    data['colorList'] = colorList?.map((e) => e.value).toList();
    data['smearOperation'] = smearOperation?.toJson();
    return data;
  }
}
